package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.*;
import com.atguigu.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author:ahang
 * @create:2022-07-25 18:14
 * @Description:
 */
@Controller
@RequestMapping("/house")
public class HouseController extends BaseController {

    @Reference
    private HouseService houseService;

    @Reference
    private CommunityService communityService;

    @Reference
    private DictService dictService;

    @RequestMapping
    public String index(HttpServletRequest request, Model model){
        //获取查询条件和分页信息
        Map<String, Object> filters = super.getFilters(request);
        model.addAttribute("filters",filters);
        //查询符合条件的当前页的数据
        PageInfo<House> page = houseService.findPage(filters);
        model.addAttribute("page",page);
        //查询小区列表
        List<Community> communityList = communityService.findAll();
        model.addAttribute("communityList",communityList);
        //查询6个字典项列表
        List<Dict> houseTypeList = dictService.findListByDictCode("houseType");
        model.addAttribute("houseTypeList",houseTypeList);
        List<Dict> floorList = dictService.findListByDictCode("floor");
        model.addAttribute("floorList",floorList);
        List<Dict> buildStructureList = dictService.findListByDictCode("buildStructure");
        model.addAttribute("buildStructureList",buildStructureList);
        List<Dict> directionList = dictService.findListByDictCode("direction");
        model.addAttribute("directionList",directionList);
        List<Dict> decorationList = dictService.findListByDictCode("decoration");
        model.addAttribute("decorationList",decorationList);
        List<Dict> houseUseList = dictService.findListByDictCode("houseUse");
        model.addAttribute("houseUseList",houseUseList);
        //跳转页面
        return "house/index";
    }

    @RequestMapping("/create")
    public String create(Model model){
        //查询小区列表
        List<Community> communityList = communityService.findAll();
        model.addAttribute("communityList",communityList);
        //查询6个字典项列表
        List<Dict> houseTypeList = dictService.findListByDictCode("houseType");
        model.addAttribute("houseTypeList",houseTypeList);
        List<Dict> floorList = dictService.findListByDictCode("floor");
        model.addAttribute("floorList",floorList);
        List<Dict> buildStructureList = dictService.findListByDictCode("buildStructure");
        model.addAttribute("buildStructureList",buildStructureList);
        List<Dict> directionList = dictService.findListByDictCode("direction");
        model.addAttribute("directionList",directionList);
        List<Dict> decorationList = dictService.findListByDictCode("decoration");
        model.addAttribute("decorationList",decorationList);
        List<Dict> houseUseList = dictService.findListByDictCode("houseUse");
        model.addAttribute("houseUseList",houseUseList);

        return "house/create";
    }

    @RequestMapping("/save")
    public String insert(House house){
        houseService.insert(house);

        return "common/successPage";
    }

    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model){
        //根据id查询房源信息
        House house = houseService.getById(id);
        model.addAttribute("house",house);


        //查询小区列表
        List<Community> communityList = communityService.findAll();
        model.addAttribute("communityList",communityList);
        //查询6个字典项列表
        List<Dict> houseTypeList = dictService.findListByDictCode("houseType");
        model.addAttribute("houseTypeList",houseTypeList);
        List<Dict> floorList = dictService.findListByDictCode("floor");
        model.addAttribute("floorList",floorList);
        List<Dict> buildStructureList = dictService.findListByDictCode("buildStructure");
        model.addAttribute("buildStructureList",buildStructureList);
        List<Dict> directionList = dictService.findListByDictCode("direction");
        model.addAttribute("directionList",directionList);
        List<Dict> decorationList = dictService.findListByDictCode("decoration");
        model.addAttribute("decorationList",decorationList);
        List<Dict> houseUseList = dictService.findListByDictCode("houseUse");
        model.addAttribute("houseUseList",houseUseList);



        return "house/edit";
    }

    @RequestMapping("/update")
    public String update(House house){
        houseService.update(house);

        return "common/successPage";
    }


    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable Long id){
        houseService.delete(id);
        return "redirect:/house";
    }

    @RequestMapping("/publish/{id}/{status}")
    public String publish(@PathVariable Long id,@PathVariable int status){
        houseService.publish(id,status);
        return "redirect:/house";
    }

    @Reference
    private HouseImageService houseImageService;
    @Reference
    private HouseBrokerService houseBrokerService;
    @Reference
    private  HouseUserService houseUserService;

    @RequestMapping("/detail/{houseId}")
    public String showDetial(@PathVariable Long houseId,Model model){
        //  1、房源信息
        House house = houseService.getById(houseId);
        model.addAttribute("house",house);
        //​	2、小区信息
        Community community = communityService.getById(house.getCommunityId());
        model.addAttribute("community",community);
        //​	3、房源图片信息（前台展示的），对应表：hse_house_image（type=1）
        List<HouseImage> houseImage1List = houseImageService.findList(houseId, 1);
        model.addAttribute("houseImage1List",houseImage1List);
        //​	4、房产图片信息（后台经纪人收集的信息，不对外发布），对应表：hse_house_image（type=2）
        List<HouseImage> houseImage2List = houseImageService.findList(houseId, 2);
        model.addAttribute("houseImage2List",houseImage2List);
        //​	5、经纪人信息，对应表：hse_house_broker
        List<HouseBroker> listByHouseId1 = houseBrokerService.findListByHouseId(houseId);
        model.addAttribute("houseBrokerList",listByHouseId1);
        //​	6、房东信息，对应表：hse_house_user
        List<HouseUser> listByHouseId = houseUserService.findListByHouseId(houseId);
        model.addAttribute("houseUserList",listByHouseId);

        return "house/show";
    }
}
